package com.itheima.reggie.service.Impl;

import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    //进行依赖注入
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    /**
     * 生成订单信息
     * <p>
     * 加入事务
     *
     * @param orders
     * @param userId
     */
    @Transactional
    @Override
    public void addOrder(Orders orders, Long userId) {
        //根据收货地址id获取地址对象AddressBook
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        //根据登录用户获取购物车数据
        List<ShoppingCart> cartList = shoppingCartMapper.findAllUserId(userId);
        //根据登录用户id获取用户名写入订单中
        User user = userMapper.selectById(userId);

        //完善数据
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        orders.setId(oid);
        orders.setNumber(oid + "");
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());

        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());  //获取登录用户的名字
        orders.setConsignee(addressBook.getConsignee()); //获取收件人信息

        //完善订单明细表数据
        ArrayList<OrderDetail> list = new ArrayList<>();
        //定义总价钱
        BigDecimal total = new BigDecimal("0");

        for (ShoppingCart shoppingCart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail.setId(orderDetailId);
            orderDetail.setOrderId(orders.getId());
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());

            //封装orderDetail对象添加到list中
            list.add(orderDetail);

            //计算总价
            total = total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber() + "")));
        }
        //将总价添加到订单中
        orders.setAmount(total);
        //添加订单数据
        orderMapper.save(orders);
        //添加订单明细数据
        orderDetailMapper.saveBatch(list);
        //订单生成后清空购物车
        shoppingCartMapper.deleteByUserId(userId);
    }
}
